﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using RentAWorker.Web.Factory;
using RentAWorker.Business.Interfaces;
using System.Web.Security;
using System.Security.Principal;

namespace RentAWorker.WebNew
{
    public partial class Register : RentPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RegisterCustomer_CreatedUser(object sender, EventArgs e)
        {
            try
            {
                // Get custom textbox from wizard step
                TextBox CompanyNameText = RegisterCustomer.CreateUserStep.ContentTemplateContainer.FindControl("CompanyNameText") as TextBox;

                IPrincipal Current = System.Threading.Thread.CurrentPrincipal;
                try
                {
                    // The user is currently not authenticated
                    // For "in-process-hosting" the web application needs to act as "registering" user
                    System.Threading.Thread.CurrentPrincipal =
                        new GenericPrincipal(new GenericIdentity("RentAWorker.WebNew"),
                                             new string[] { "FrontOffice" });

                    ICustomerService RegService = ClientProxyFactory.GetCustomerProxy();
                    RegService.RegisterNewCustomer
                    (
                        (Guid)(Membership.GetUser(RegisterCustomer.UserName).ProviderUserKey),
                        CompanyNameText.Text
                    );
                }
                finally
                {
                    System.Threading.Thread.CurrentPrincipal = Current;
                }
            }
            catch (Exception ex)
            {
                LegendError.Text = "Error occured, please contact your administrator!";
                EventLog.WriteEntry
                    (
                        "Application",
                        "Error in our web application: "
                        + ex.Message + Environment.NewLine
                        + ex.ToString()
                    );
            }
        }

        protected void RegisterCustomer_CreatingUser(object sender, LoginCancelEventArgs e)
        {

        }
    }
}
